#pragma once
#include "HashBucket.h"

namespace My_unordered_map
{
	template<class K, class V, class Hash = HashFunc<K>>
	class unordered_map
	{
		struct MapKeyOfT
		{
			const K& operator()(const pair<K, V>& kv)
			{
				return kv.first;
			}
		};
	public:
		typedef typename Hash_Bucket::HashTable<K, pair<const K, V>, MapKeyOfT, Hash>::Iterator iterator;
		typedef typename Hash_Bucket::HashTable<K, pair<const K, V>, MapKeyOfT, Hash>::ConstIterator const_iterator;
		unordered_map() = default;
		unordered_map(initializer_list<pair<K,V>> il)
		{
			for (const auto& e : il)
			{
				_ht.Insert(e);
			}
		}
		template<class InputIterator>
		unordered_map(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				_ht.Insert(*first);
				++first;
			}
		}
		unordered_map(const unordered_map& x)
		{
			for (const auto& e : x)
			{
				_ht.Insert(e);
			}
		}
		unordered_map& operator=(const unordered_map& x)
		{
			_ht = x._ht;
			return *this;
		}
		pair<iterator,bool> insert(const pair<K, V>& kv)
		{
			return _ht.Insert(kv);
		}
		V& operator[](const K& key)
		{
			pair<iterator, bool> ret = insert({ key,V() });
			return ret.first->second;
		}
		bool erase(const K& key)
		{
			return _ht.Erase(key);
		}
		iterator begin()
		{
			return _ht.Begin();
		}
		iterator end()
		{
			return _ht.End();
		}
		const_iterator begin() const
		{
			return _ht.Begin();
		}
		const_iterator end() const
		{
			return _ht.End();
		}
		iterator find(const K& key)
		{
			return _ht.Find(key);
		}
		size_t size() const
		{
			return _ht.Size();
		}
		bool empty() const
		{
			return _ht.Empty();
		}
		size_t count(const K& key)
		{
			return _ht.Count(key);
		}
		size_t bucket_count()
		{
			return _ht.Bucket_count();
		}
		size_t bucket_size(const K& key)
		{
			return _ht.Bucket_size(key);
		}
	private:
		Hash_Bucket::HashTable<K, pair<const K, V>, MapKeyOfT, Hash> _ht;
	};

	void test1_map()
	{
		//const unordered_map<int, int> mp;
		unordered_map<int, int> mp;
		mp.insert({ 1,1 });
		mp.insert({ 2,2 });
		mp.insert({ 3,4 });
		mp.insert({ 54,54 });
		for (auto& e : mp)
		{
			cout << e.first << ":" << e.second << endl;
		}
	}
	void test2_map()
	{
		unordered_map<string, string> dict;
		dict.insert({ "sort", "排序" });
		dict.insert({ "left", "左边" });
		dict.insert({ "right", "右边" });
		dict["left"] = "左边，剩余";
		dict["insert"] = "插入";
		dict["string"];
		unordered_map<string, string>::iterator it = dict.begin();
		while (it != dict.end())
		{
			// 不能修改first，可以修改second
			//it->first += 'x';
			it->second += 'x';
			cout << it->first << ":" << it->second << endl;
			++it;
		}
		cout << endl;
	}
	void test3_map()
	{
		unordered_map<string, string> mp1 = { { "sort", "排序" } ,{ "left", "左边" } ,{ "right", "右边" } };
		unordered_map<string, string>::iterator it = mp1.begin();
		while (it != mp1.end())
		{
			cout << it->first << ":" << it->second << endl;
			++it;
		}
		cout << endl;

		unordered_map<string, string> mp2 = mp1;
		mp2.insert({ "insert","插入" });
		it = mp2.begin();
		while (it != mp2.end())
		{
			cout << it->first << ":" << it->second << endl;
			++it;
		}
		cout << endl;

		mp1 = mp2;
		it = mp1.begin();
		while (it != mp1.end())
		{
			cout << it->first << ":" << it->second << endl;
			++it;
		}
		cout << endl;
	}
}